#include "showcase.h"
#include "ui_showcase.h"
#include <QDebug>
showCase::showCase(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::showCase)
{
    ui->setupUi(this);
//    ui->personalInfo->setColumnWidth(0, 600);
//    ui->medicines->setColumnWidth(0,200);
    ui->personalInfo->horizontalHeader()->setStretchLastSection(true);
    ui->medicines->horizontalHeader()->setStretchLastSection(true);
    connect(ui->saveBtn,&QPushButton::clicked,this,&showCase::saveCase);
    connect(&timer,&QTimer::timeout,this,&showCase::autoSave);
    timer.setInterval(300000);
}

void showCase::autoSave()
{
    qDebug()<<"auto_saving";
    for(int i=0;i<21;i++)
    {
        qDebug()<<i;
        QString value = ui->personalInfo->model()->index(i,0).data().toString();
        qDebug()<<i;
        qDebug()<<num;
        qDebug()<<value;
        qDebug()<<l->items[i];
        (*l)[num][l->items[i]]=value;
        qDebug()<<i;
    }
    qDebug()<<"auto_saving";
    for(int i=0;i<25;i++)
    {
        QString value = ui->medicines->model()->index(i,0).data().toString();
        (*l)[num].meds[i]=value;
    }
    qDebug()<<"auto_saving";
    QString value = ui->personalInfo->model()->index(21,0).data().toString();
    (*l)[num]["pacNum"]=value;
    value = ui->personalInfo->model()->index(22,0).data().toString();
    (*l)[num]["note"]=value;
    (*l)[num].countMedsnum();
    qDebug()<<"auto_saving";
    l->exportToExcel();
    qDebug()<<"auto_saving";
}

showCase::~showCase()
{
    delete ui;
}

void showCase::init(illcases *p,int numm)
{
    qDebug()<<"initializing...:"<<numm;
    timer.start();
    l=p;
    num=numm;
    qDebug()<<"initializing...";
    QTableWidgetItem *pp[30];
    for(int j=0;j<21;j++)
        pp[j]=new QTableWidgetItem;
    qDebug()<<"initializing...";
    for(int j=0;j<21;j++)
    {
        qDebug()<<j;
        qDebug()<<"item:"<<(*l)[numm][l->items[j]];
        pp[j]->setText((*l)[numm][l->items[j]]);
    }
    qDebug()<<"initializing...";
    for(int j=0;j<21;j++)
        ui->personalInfo->setItem(j,0,pp[j]);
    qDebug()<<"initializing...";
    QTableWidgetItem *mm[30];
    for(int j=0;j<25;j++)
        mm[j]=new QTableWidgetItem;
    qDebug()<<"initializing...";
    for(int j=0;j<25;j++)
    {
        qDebug()<<j;
        mm[j]->setText((*l)[numm].meds[j]);
    }
    qDebug()<<"initializing...";
    for(int j=0;j<25;j++)
        ui->medicines->setItem(j,0,mm[j]);
    qDebug()<<"initializing...";
    QTableWidgetItem *ppp[2];
    for(int j=0;j<2;j++)
        ppp[j]=new QTableWidgetItem;
    ppp[0]->setText((*l)[numm]["pacNum"]);
    qDebug()<<(*l)[numm]["note"];
    ppp[1]->setText((*l)[numm]["note"]);
    for(int j=0;j<2;j++)
        ui->personalInfo->setItem(j+21,0,ppp[j]);
    qDebug()<<"initializing...";
}

void showCase::saveCase()
{
    QString IDD=ui->personalInfo->model()->index(6,0).data().toString();
    if(IDD=="")
    {
        QPushButton *okbtn = new QPushButton("确定");
        QMessageBox *mymsgbox = new QMessageBox;
        mymsgbox->setIcon(QMessageBox::Warning);
        mymsgbox->setWindowTitle("提示");
        mymsgbox->setText("未填写门诊号");
        mymsgbox->addButton(okbtn, QMessageBox::AcceptRole);
        mymsgbox->show();
        mymsgbox->exec();//等待用户响应
        if (mymsgbox->clickedButton()==okbtn)//点击了OK按钮
        {
            mymsgbox->hide();
        }
        return;
    }
    for(int i=0;i<21;i++)
    {
        qDebug()<<i;
        QString value = ui->personalInfo->model()->index(i,0).data().toString();
        qDebug()<<i;
        qDebug()<<num;
        qDebug()<<l->items[i];
        qDebug()<<value;
        (*l)[num][l->items[i]]=value;
        qDebug()<<i;
    }
    for(int i=0;i<25;i++)
    {
        QString value = ui->medicines->model()->index(i,0).data().toString();
        (*l)[num].meds[i]=value;
    }
    QString value = ui->personalInfo->model()->index(21,0).data().toString();
    (*l)[num]["pacNum"]=value;
    value = ui->personalInfo->model()->index(22,0).data().toString();
    (*l)[num]["note"]=value;
    (*l)[num].countMedsnum();
    l->exportToExcel();
    this->hide();
    this->saved();
    timer.stop();
}
